package com.hnzq.cloud.freshorderassembly.util;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class WoniuBeanUtils {

    public static  <T> T copyObject(Object source,Class<T> type){
        T obj = null;
        try {
            obj = type.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        BeanUtils.copyProperties(source,obj);
        return obj;
    }

    public static <T> List<T> copyList(Collection source,Class<T> type){
        ArrayList<T> list = new ArrayList<>();
        for (Object obj : source) {
            try {
               T t = type.newInstance();
                BeanUtils.copyProperties(obj,t);
                list.add(t);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

}
